package net.oschina.git.fsppp.model;


/**
 * 文件目录(文件)
 * 文件即目录
 */
public class P3Dir implements Cloneable {
    /**
     * String: name
     * 目录（文件）名
     */
    private String name;
    /**
     * String: type
     * 目录类型
     * 1 文件夹
     * 0 文件
     */
    private int type;
    /**
     * String: attr
     * 目录属性
     * .txt
     * .exe etc.
     */
    private String attr;
    /**
     * int: startNum
     * startNum=0表示已删除
     * 起始块号
     */
    private int startNum;
    /**
     * int: length
     * 文件长度
     */
    private int length;
    /**
     * 父文件（夹） id
     */
    private int pid;

    /**
     * path: String
     * 路径
     */
    private String path;

    /**
     * content: Byte
     * 文件内容
     * 如果是文件夹则为null
     */
    private Byte[] content;

    /**
     * 文件是否打开
     */
    private boolean isOpen = false;
    public static final int TYPE_OF_FILE = 0;
    public static final int TYPE_OF_DIR = 1;
    public P3Dir() {
    }

    public P3Dir(String name, byte type, String attr, int startNum, int length) {
        this.name = name;
        this.type = type;
        this.attr = attr;
        this.startNum = startNum;
        this.length = length;
    }

    public boolean isEmpty() {
        return (
                this.name != null
                && this.name.length() > 0
                && this.name.startsWith("$")
        );
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getAttr() {
        return attr;
    }

    public void setAttr(String attr) {
        this.attr = attr;
    }

    public int getStartNum() {
        return startNum;
    }

    public void setStartNum(int startNum) {
        this.startNum = startNum;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Byte[] getContent() {
        return content;
    }

    public void setContent(Byte[] content) {
        this.content = content;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setIsOpen(boolean isOpen) {
        this.isOpen = isOpen;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        P3Dir dir = (P3Dir) super.clone();
        if (this.content != null) {
            dir.content = dir.content.clone();
        }
        return dir;
    }

    @Override
    public int hashCode() {
        int hash = 17;

        if (this.name != null) {
            hash = hash * 31 + this.name.hashCode();
        }

        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || !this.getClass().equals(obj.getClass())) {
            return false;
        }

        P3Dir p = (P3Dir) obj;

        return (
                this.name != null &&
                        this.name.equals(p.getName())
        );
    }

    @Override
    public String toString() {
        return this.name;
    }
}
